<?php        
    require_once(dirname(__DIR__).
        DIRECTORY_SEPARATOR.
        "inc".
        DIRECTORY_SEPARATOR.
        "incFunctions.php"
    );
    
    use SharedPHP\TestSuite;
    
    ### Testataan käyttäjän luominen
    ### ----------------------------
    
    # Luodaan kaksi testikäyttäjää rajapintaan    
    $user1Passwd    = uniqid();
    $ses1           = null;
    $user1          = array(
        "email"         => time().".".uniqid()."@email.com",
        "timezoneid"    => null,
        "password"      => $user1Passwd,
        "password2"     => $user1Passwd
    );
    
    $user2Passwd    = uniqid();
    $ses2           = null;
    $user2          = array(
        "email"         => time().".".uniqid()."@email.com",
        "timezoneid"    => null,
        "password"      => $user2Passwd,
        "password2"     => $user2Passwd
    );
    
    # Haetaan aikahyöhykkeet
    TestSuite::test("flow-1.0",function() use (&$user1,&$user2){
        $result = _apiCall("TimeZone","getList");
        
        # Aseta aikahyöhykkeet käyttäjille
        if (!empty($result["data"])) {
            $user1["timezoneid"] = array_keys($result["data"])[0];
            $user2["timezoneid"] = array_keys($result["data"])[0];
        }
        
        return (bool)count($result["data"]);
    },true);
    
    # Luo 1. käyttäjä rajapintaan
    TestSuite::test("flow-1.1",function() use ($user1){
        $result = _apiCall("User","createUser",null,$user1);
        return $result["code"];
    },0);
    
    # Luo 2. käyttäjä rajapintaan
    TestSuite::test("flow-1.2",function() use ($user2){
        $result = _apiCall("User","createUser",null,$user2);
        return $result["code"];
    },0);
    
    ### Testataan käyttäjän kirjautuminen
    ### ---------------------------------
    
    # Kirjaa 1. käyttäjä sisään
    TestSuite::test("flow-2.0",function() use ($user1,&$ses1){
        $result = _apiCall("Login","doLogin",null,array(
            "email"     => $user1["email"],
            "password"  => $user1["password"]
        ));
        $ses1 = $result["data"];
        return $result["code"];
    },0);
    
    # Kirjaa 2. käyttäjä sisään
    TestSuite::test("flow-2.1",function() use ($user2,&$ses2){
        $result = _apiCall("Login","doLogin",null,array(
            "email"     => $user2["email"],
            "password"  => $user2["password"]
        ));
        $ses2 = $result["data"];
        return $result["code"];
    },0);
    
    ### Testataan templaten luominen
    ### ----------------------------
    
    $exercises = array();
    
    # Haetaan harjoitteet
    TestSuite::test("flow-3.0",function() use ($ses1,&$exercises){
        $result = _apiCall("Exercise","getList",$ses1);
        $exercises = $result["data"];
        return (bool)count($result["data"]);
    },true);
    
    # Luodaan template
    $arr = $exercises[array_keys($exercises)[0]];
    $arr = $arr[array_keys($arr)[0]];    
    $template = array(
        "extratext" => uniqid(),
        "exercises" => array(            
            array(                
                "unit"              => "kg",                
                "extratext"         => uniqid(),
                "special"           => "dropset",
                "gear_belt"         => 1,
                "gear_knee_wraps"   => 0,
                "gear_shirt"        => 1,
                "gear_suit"         => 0,
                "gear_wrist_straps" => 1,
                "gear_wrist_wraps"  => 0,
                "sets"              => array(
                    array(
                        "exerciseid"        => array_keys($arr)[0],
                        "repetitions"       => 5,
                        "repetitions_end"   => 8,
                        "weight"            => "100.00",
                        "weight_kg"         => "100.00",
                        "weight_lb"         => "220.46",
                        "onerepmax_percent" => "80.00",
                        "repetitions_type"  => "normal",
                        "barbell_type"      => "trap_bar",
                        "rest_interval_sec" => 20
                    )
                )
            )
        ),
        "records" => array(
            array(
                "record" => uniqid()
            )
        )
    );
    
    # Tallenna template
    $hashid = null;
    TestSuite::test("flow-3.1",function() use ($ses1,$template,&$hashid){
        $result = _apiCall("Template","save",$ses1,$template);
        $hashid = $result["data"];
        return $result["code"];
    },0);
    
    # Kokeile ladata template virheellisellä hashid:llä
    TestSuite::test("flow-3.2",function() use ($ses1,$hashid){
        $result = _apiCall("Template","load",$ses1,array("hashid" => $hashid."1"));        
        return array(
            "code"          => $result["code"],
            "reasonCode"    => $result["reasonCode"]
        );
    },array("code" => 20,"reasonCode" => 30));
    
    # Kokeile ladata template toiselle käyttäjälle
    TestSuite::test("flow-3.3",function() use ($ses2,$hashid){
        $result = _apiCall("Template","load",$ses2,array("hashid" => $hashid));        
        return array(
            "code"          => $result["code"],
            "reasonCode"    => $result["reasonCode"]
        );
    },array("code" => 10,"reasonCode" => 40));
    
    # Lataa template ja vertaa sitä tallennettuun
    TestSuite::test("flow-3.4",function() use ($ses1,$hashid){
        $result = _apiCall("Template","load",$ses1,array("hashid" => $hashid));
        
        $tmp = $result["data"];
        unset($tmp["hashid"]);
        unset($tmp["updated"]);
        unset($tmp["rowstamp"]);
        unset($tmp["exercises"][0]["sets"][0]["exercisetext"]);
        unset($tmp["exercises"][0]["sets"][0]["category"]);
        unset($tmp["exercises"][0]["sets"][0]["musclegroup"]);
        
        return $tmp;
    },$template);
    
    # Hae template search funktiolla
    TestSuite::test("flow-3.5",function() use ($ses1,$template){
        $result = _apiCall("Template","search",$ses1,array(
            "exerciseid" => $template["exercises"][0]["sets"][0]["exerciseid"]
        ));        
        return $result["data"]["list"][0]["hashid"];
    },$hashid);
    
    # Hae template search funktiolla toisella käyttäjällä
    TestSuite::test("flow-3.6",function() use ($ses2,$template){
        $result = _apiCall("Template","search",$ses2,array(
            "exerciseid" => $template["exercises"][0]["sets"][0]["exerciseid"]
        ));        
        return count($result["data"]["list"]);
    },0);
    
    ### Testataan harjoitusohjelman luominen
    ### ------------------------------------
    
    # Luo harjoitusohjelma
    $time = time();
    $tp = array(
        "program_name"  => uniqid(),
        "startdate"     => date("Y-m-d",$time),
        "enddate"       => date("Y-m-d",strtotime("+1 day",$time)),
        "templatelink"  => array(
            array(
                "templatehashid"        => $hashid,
                "days_to_next_workout"  => 1
            ),
        ),
        "records" => array(
            array(
                "record" => uniqid()
            )
        )
    );
    
    $tpHashid = null;
    TestSuite::test("flow-4.0",function() use ($ses1,$tp,&$tpHashid){
        $result = _apiCall("TrainingProgram","save",$ses1,$tp);
        $tpHashid = $result["data"];
        return $result["code"];
    },0);
    
    # Kokeile ladata harjoitusohjelma virheellisellä hashid:llä
    TestSuite::test("flow-4.1",function() use ($ses1,$tpHashid){
        $result = _apiCall("TrainingProgram","load",$ses1,array("hashid" => $tpHashid."1"));        
        return array(
            "code"          => $result["code"],
            "reasonCode"    => $result["reasonCode"]
        );
    },array("code" => 20,"reasonCode" => 30));
    
    # Kokeile ladata harjoitusohjelma toiselle käyttäjälle (virhe)
    TestSuite::test("flow-4.2",function() use ($ses2,$tpHashid){
        $result = _apiCall("TrainingProgram","load",$ses2,array("hashid" => $tpHashid));        
        return array(
            "code"          => $result["code"],
            "reasonCode"    => $result["reasonCode"]
        );
    },array("code" => 10,"reasonCode" => 40));
    
    # Lataa harjoitusohjelma ja vertaa sitä tallennettuun
    $tpLoad = null;
    TestSuite::test("flow-4.3",function() use ($ses1,$tpHashid,&$tpLoad){
        $result = _apiCall("TrainingProgram","load",$ses1,array("hashid" => $tpHashid));
        
        $tpLoad = $tmp = $result["data"];
        unset($tmp["hashid"]);
        unset($tmp["workout_count"]);
        unset($tmp["updated"]);
        unset($tmp["rowstamp"]);        
        unset($tmp["templatelink"][0]["template_extratext"]);
        
        return $tmp;
    },$tp);
    
    # Katso että harjoitusohjelmassa on kaksi salikäyntiä
    TestSuite::test("flow-4.4",function() use ($tpLoad){        
        return $tpLoad["workout_count"];
    },2);
    
    # Hae harjoitusohjelma search funktiolla
    TestSuite::test("flow-4.5",function() use ($ses1,$hashid){
        $result = _apiCall("TrainingProgram","search",$ses1,array(
            "templatehashid" => $hashid
        ));
        return $result["data"]["list"][0]["hashid"];
    },$tpHashid);
    
    # Hae harjoitusohjelma search funktiolla toisella käyttäjällä (virhe)
    TestSuite::test("flow-4.6",function() use ($ses2,$hashid){
        $result = _apiCall("TrainingProgram","search",$ses2,array(
            "templatehashid" => $hashid
        ));        
        return count($result["data"]["list"]);
    },0);
    
    # Hae harjoitusaikataulu schedule funktiolla
    TestSuite::test("flow-4.7",function() use ($ses1,$hashid){
        $result = _apiCall("TrainingProgram","schedule",$ses1,array(
            "templatehashid" => $hashid
        ));
        return $result["data"]["list"][1]["workout_date"];
    },$tp["enddate"]);
    
    # Hae harjoitusaikataulu schedule funktiolla toisella käyttäjällä (virhe)
    TestSuite::test("flow-4.8",function() use ($ses2,$hashid){
        $result = _apiCall("TrainingProgram","schedule",$ses2,array(
            "templatehashid" => $hashid
        ));        
        return count($result["data"]["list"]);
    },0);
    
    ### Testataan harjoituksen luominen
    ### -------------------------------
    
    $time = time();
    $workout = $template;
    $workout["locationtext"]                    = uniqid();
    $workout["training_program_hashid"]         = $tpHashid;
    $workout["templatehashid"]                  = $hashid;
    $workout["starttime"]                       = date("Y-m-d H:i:s",$time);
    $workout["endtime"]                         = date("Y-m-d H:i:s",strtotime("+1 hour",$time));
    $workout["user_weight"]                     = array();
    $workout["user_weight"]["weight"]           = 80.00;
    $workout["user_weight"]["weight_kg"]        = 80.00;
    $workout["user_weight"]["weight_lb"]        = 176.37;
    $workout["user_weight"]["unit"]             = "kg";
    $workout["user_weight"]["measurement_time"] = "morning";
    $workout["user_weight"]["fatpercent"]       = 13.50;
    $workout["user_weight"]["date"]             = date("Y-m-d",$time);
    unset($workout["exercises"][0]["sets"][0]["repetitions_end"]);
    unset($workout["exercises"][0]["sets"][0]["onerepmax_percent"]);
    
    # Tallenna harjoitus
    $workoutHashid = null;
    TestSuite::test("flow-5.0",function() use ($ses1,$workout,&$workoutHashid){
        $result = _apiCall("Workout","save",$ses1,$workout);
        $workoutHashid = $result["data"];
        return $result["code"];
    },0);
    
    # Kokeile ladata harjoitus virheellisellä hashid:llä
    TestSuite::test("flow-5.1",function() use ($ses1,$workoutHashid){
        $result = _apiCall("Workout","load",$ses1,array("hashid" => $workoutHashid."1"));        
        return array(
            "code"          => $result["code"],
            "reasonCode"    => $result["reasonCode"]
        );
    },array("code" => 20,"reasonCode" => 30));
    
    # Kokeile ladata harjoitus toiselle käyttäjälle
    TestSuite::test("flow-5.2",function() use ($ses2,$workoutHashid){
        $result = _apiCall("Workout","load",$ses2,array("hashid" => $workoutHashid));        
        return array(
            "code"          => $result["code"],
            "reasonCode"    => $result["reasonCode"]
        );
    },array("code" => 10,"reasonCode" => 40));
    
    # Lataa harjoitus ja vertaa sitä tallennettuun
    $locationHashid = null;
    $userWeightHashid = null;
    TestSuite::test("flow-5.3",function() use ($ses1,$workoutHashid,&$locationHashid,&$userWeightHashid){
        $result = _apiCall("Workout","load",$ses1,array("hashid" => $workoutHashid));
        
        $tmp = $result["data"];
        $locationHashid = $tmp["locationhashid"];
        $userWeightHashid = $tmp["user_weight"]["hashid"];
        unset($tmp["hashid"]);
        unset($tmp["locationhashid"]);
        unset($tmp["training_program_name"]);
        unset($tmp["template_extratext"]);
        unset($tmp["updated"]);
        unset($tmp["rowstamp"]);
        unset($tmp["user_weight"]["hashid"]);
        unset($tmp["user_weight"]["updated"]);
        unset($tmp["user_weight"]["rowstamp"]);
        unset($tmp["exercises"][0]["sets"][0]["exercisetext"]);
        unset($tmp["exercises"][0]["sets"][0]["category"]);
        unset($tmp["exercises"][0]["sets"][0]["musclegroup"]);
        
        return $tmp;
    },$workout);
    
    # Hae harjoitus search funktiolla
    TestSuite::test("flow-5.4",function() use ($ses1,$hashid){
        $result = _apiCall("Workout","search",$ses1,array(
            "templatehashid" => $hashid
        ));
        return $result["data"]["list"][0]["hashid"];
    },$workoutHashid);
    
    # Hae harjoitus search funktiolla toisella käyttäjällä (virhe)
    TestSuite::test("flow-5.5",function() use ($ses2,$hashid){
        $result = _apiCall("Workout","search",$ses2,array(
            "templatehashid" => $hashid
        ));        
        return count($result["data"]["list"]);
    },0);
    
    # Hae harjoitusten yhden toiston maksimin kehitys
    TestSuite::test("flow-5.6",function() use ($ses1){
        $result = _apiCall("Workout","oneRepMax",$ses1,array(
            "calculated" => 1
        ));
        return $result["data"][0]["hashid"];
    },$workoutHashid);
    
    # Hae harjoitusten yhden toiston maksimin kehitys toisella käyttäjällä (virhe)
    TestSuite::test("flow-5.7",function() use ($ses2,$hashid){
        $result = _apiCall("Workout","oneRepMax",$ses2,array(
            "templatehashid" => $hashid
        ));        
        return count($result["data"]);
    },0);
    
    # Hae painotiedot getUserWeight funktiolla
    TestSuite::test("flow-5.8",function() use ($ses1,$userWeightHashid){
        $result = _apiCall("User","getUserWeight",$ses1,array(
            "hashid" => $userWeightHashid
        ));
        return $result["data"]["list"][0]["hashid"];
    },$userWeightHashid);
    
    # Hae painotiedot getUserWeight funktiolla toisella käyttäjällä (virhe)
    TestSuite::test("flow-5.9",function() use ($ses2,$userWeightHashid){
        $result = _apiCall("User","getUserWeight",$ses2,array(
            "hashid" => $userWeightHashid
        ));        
        return count($result["data"]["list"]);
    },0);
    
    ### Testataan syötetyn datan poisto
    ### -------------------------------
    
    # Poista harjoitus
    TestSuite::test("flow-6.0",function() use ($ses1,$workoutHashid){
        $result = _apiCall("Workout","delete",$ses1,array("hashid" => $workoutHashid));        
        return $result["code"];
    },0);
    
    # Poista harjoitusohjelma
    TestSuite::test("flow-6.1",function() use ($ses1,$tpHashid){
        $result = _apiCall("TrainingProgram","delete",$ses1,array("hashid" => $tpHashid));        
        return $result["code"];
    },0);
    
    # Poista template
    TestSuite::test("flow-6.2",function() use ($ses1,$hashid){
        $result = _apiCall("Template","delete",$ses1,array("hashid" => $hashid));        
        return $result["code"];
    },0);
    
    # Poista harjoituspaikka
    TestSuite::test("flow-6.3",function() use ($ses1,$locationHashid){
        $result = _apiCall("Location","delete",$ses1,array("hashid" => $locationHashid));        
        return $result["code"];
    },0);
    
    # Hae harjoitus search funktiolla -> ei osumia
    TestSuite::test("flow-6.4",function() use ($ses1){
        $result = _apiCall("Workout","search",$ses1);        
        return count($result["data"]["list"]);
    },0);
    
    # Hae harjoitusohjelma search funktiolla -> ei osumia
    TestSuite::test("flow-6.5",function() use ($ses1){
        $result = _apiCall("TrainingProgram","search",$ses1);        
        return count($result["data"]["list"]);
    },0);
    
    # Hae template search funktiolla -> ei osumia
    TestSuite::test("flow-6.6",function() use ($ses1){
        $result = _apiCall("Template","search",$ses1);        
        return count($result["data"]["list"]);
    },0);
    
    # Hae harjoituspaikka search funktiolla -> ei osumia
    TestSuite::test("flow-6.7",function() use ($ses1){
        $result = _apiCall("Location","getList",$ses1);        
        return count($result["data"]);
    },0);
    
    # Poista painotieto
    TestSuite::test("flow-6.8",function() use ($ses1,$userWeightHashid){
        $result = _apiCall("User","deleteUserWeight",$ses1,array(
            "hashid" => $userWeightHashid
        ));
        return $result["code"];
    },0);
    
    # Hae painotieto getUserWeight funktiolla -> ei osumia
    TestSuite::test("flow-6.9",function() use ($ses1){
        $result = _apiCall("User","getUserWeight",$ses1);        
        return count($result["data"]["list"]);
    },0);
    
    ### Kirjataan käyttäjät ulos
    ### ------------------------
    
    # Kirjaa 1. käyttäjä ulos
    TestSuite::test("flow-7.0",function() use ($ses1){
        $result = _apiCall("Login","doLogout",$ses1);
        return $result["code"];
    },0);
    
    # Kirjaa 2. käyttäjä ulos
    TestSuite::test("flow-7.1",function() use ($ses2){
        $result = _apiCall("Login","doLogout",$ses2);
        return $result["code"];
    },0);